package com.cjst.controller.subject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.subject.ExamOption;
import com.cjst.pojo.subject.ExamResult;
import com.cjst.pojo.subject.QuestClass;
import com.cjst.pojo.subject.Subjects;
import com.cjst.pojo.sys.SysUser;
import com.cjst.service.subject.ExamOptionService;
import com.cjst.service.subject.ExamResultService;
import com.cjst.service.subject.QuestClassService;
import com.cjst.service.subject.SubjectsService;
import com.cjst.service.sys.SysUserService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.UserPrincipalUtil;
import com.cjst.util.code.PageUtil;
import com.cjst.util.common.DateUtil;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("subjects")
public class SubjectsController extends BaseController{
	protected final Logger log = Logger.getLogger(SubjectsController.class);
	@Autowired
	private SubjectsService subjectsService;
	@Autowired
	private ExamOptionService examOptionService;
	@Autowired
	private QuestClassService questClassService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private ExamResultService examResultService;
	
	
	private static String[] examineTarget= {"水体","公厕","环境","其它"};
	private static int[] examineTimes= {1,2,3,4,5,6,7}; 
	private static String[] examineType= {"每周检查","每月检查","每月随机检查","每双月检查"};
	
	
	@RequestMapping("")
	public String index(Model model) {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		WherePrams wheres=Method.where("isDelete", C.EQ, 0);
		UserPrincipalUtil.getPriceMegod(wheres, sysUser, "createUser");
		wheres.orderBy(" create_time desc ");
		/*List<SchoolCourse> courseList=schoolCourseService.list(wheres);*/
		List<QuestClass> qcList=questClassService.list(wheres);
		model.addAttribute("qcList", qcList);
		return "subjects/subjects/subjects";
	}
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String catagoryId, String content) {
		WherePrams wheres=Method.where("isDelete", C.EQ, 0);
		if(catagoryId!=null && catagoryId.length()>0){
			wheres.and("subNo", C.EQ, catagoryId);
		}
		if(content!=null && content.length()>0){
			wheres.and("content", C.LIKE, content);
		}
		wheres.orderBy(" sort asc");
		PageUtil<Subjects> page=subjectsService.listPageCount(wheres);
		return super.packForBT(page);
	}
	@RequestMapping("subjects_add")
	public String subjectsAdd(Model model) {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		WherePrams wheres=Method.where("delFlag", C.EQ, 0);
		UserPrincipalUtil.getPriceMegod(wheres, sysUser, "userId");
		
		WherePrams wheres2=Method.where("isDelete", C.EQ, 0);
		List<QuestClass> questClassList= questClassService.list(wheres2);
		model.addAttribute("questClassList", questClassList);
		
		
		return "subjects/subjects/subjects_add";
	}
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	//@RequiresPermissions("ks_add")
	public Object add(Subjects subjects, HttpServletRequest req) throws Exception {
		 
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
	    subjects.setCreateTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
	    subjects.setCreateUser(sysUser.getId());
	    QuestClass qc=questClassService.get(subjects.getCatagoryId());
	    subjects.setSubNo(qc.getClassName());
	    String flag = subjectsService.addLocalByid(subjects);
	    //updateRedisUser();
	    log.info("============保存成功===============");
	    return new ResultObj(0, "保存成功", flag);
	}
	@RequestMapping(value = "/subjects_option/{id}", method = RequestMethod.GET)
	public String subjects_option(@PathVariable String id,Model model) {
		Subjects subject=subjectsService.get(id);
		if(subject!=null){
			WherePrams wheres=Method.where("subNo", C.EQ, id);
			List<ExamOption> eoList=examOptionService.list(wheres);
			if(eoList!=null && eoList.size()>0){
				for (ExamOption examOption : eoList) {
					if(examOption.getOptionCode().equals("a") || examOption.getOptionCode().equals("A")){
						subject.setA(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("b") || examOption.getOptionCode().equals("B")){
						subject.setB(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("c") || examOption.getOptionCode().equals("C")){
						subject.setC(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("d") || examOption.getOptionCode().equals("D")){
						subject.setD(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("e") || examOption.getOptionCode().equals("E")){
						subject.setE(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("f") || examOption.getOptionCode().equals("F")){
						subject.setF(examOption.getOptionContent());
					}else if(examOption.getOptionCode().equals("g") || examOption.getOptionCode().equals("G")){
						subject.setG(examOption.getOptionContent());
					}
				}
			}
		}
		
		model.addAttribute("item", subject);
		return "subjects/subjects/subjects_option";
	}
	@RequestMapping(value = "/optionAdd", method = RequestMethod.POST)
	@ResponseBody
	public Object optionAdd(String a,String b,String c,String d,String e,String f,String g,String id, HttpServletRequest req) throws Exception {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if(a!=null && a.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("A");
			exo.setOptionContent(a);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(b!=null && b.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("B");
			exo.setOptionContent(b);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(c!=null && c.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("C");
			exo.setOptionContent(c);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(d!=null && d.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("D");
			exo.setOptionContent(d);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(e!=null && e.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("E");
			exo.setOptionContent(e);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(f!=null && f.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("F");
			exo.setOptionContent(f);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
		if(g!=null && g.length()>0){
			ExamOption exo=new ExamOption();
			exo.setSubNo(id);
			exo.setOptionCode("G");
			exo.setOptionContent(g);
			exo.setCreateUser(sysUser.getId());
			exo.setCreateTime(DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			examOptionService.addLocalByid(exo);
		}
	    //updateRedisUser();
	    log.info("============保存成功===============");
	    return new ResultObj(0, "保存成功", null);
	}
	
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
	public Object delete(String id,Model model) {
    	if (!StringUtils.isNotEmpty(id)) {
            return new ResultObj(1, "参数传递错误", null);
        }
        int flag=subjectsService.del(id);
         
        return new ResultObj(0, "删除成功", flag);
	}
	@RequestMapping(value = "/toUpdate/{id}", method = RequestMethod.GET)
	public String toUpdate(@PathVariable String id,Model model) {
		Subjects subject=subjectsService.get(id);
		model.addAttribute("subject", subject);
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		WherePrams wheres=Method.where("delFlag", C.EQ, 0);
		UserPrincipalUtil.getPriceMegod(wheres, sysUser, "userId");
		
		WherePrams wheres2=Method.where("isDelete", C.EQ, 0);
		wheres2.orderBy(" create_time desc");
		
		List<QuestClass> questClassList=questClassService.list(wheres2); 
		model.addAttribute("questClassList", questClassList);
		return "subjects/subjects/subjects_update";
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	//@RequiresPermissions("ks_update")
	public Object update(Subjects subjects, HttpServletRequest req) throws Exception {
		String subNo=subjects.getCatagoryId();
		if(subNo==null || subNo.length()==0){
			return new ResultObj(1, "参数错误", null);
		}
		QuestClass qc=questClassService.get(subjects.getCatagoryId());
		if(qc==null){
			return new ResultObj(1, "参数错误", null);
		}
		
		WherePrams wheres=Method.where("isDelete", C.EQ, 0);
		wheres.and("id", C.EQ, subjects.getId());
		Subjects subjectsOrigin=subjectsService.get(wheres);
		
		subjectsOrigin.setAnswer(subjects.getAnswer());
		subjectsOrigin.setCatagoryId(subjects.getCatagoryId());
		subjectsOrigin.setHelps(subjects.getHelps());
		subjectsOrigin.setRemark(subjects.getRemark());
		subjectsOrigin.setSubNo(qc.getClassName());
		subjectsOrigin.setContent(subjects.getContent());
		subjectsService.update(subjectsOrigin);
	    return new ResultObj(0, "修改成功", 0);
	}
	@RequestMapping("/result")
	public String result(Model model) {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		WherePrams wheres=Method.where("isDelete", C.EQ, 0);
		wheres.and("createUser", C.EQ, sysUser.getId());
		wheres.orderBy(" create_time desc");
		List<QuestClass> qcList=questClassService.list(wheres);
		model.addAttribute("qcList", qcList);
		return "subjects/subjects/subject_result";
	}
	@RequestMapping(value = "/result/findById")
	@ResponseBody
    public Object resultFindById(String questClassId) {
		QuestClass qc=questClassService.get(questClassId);
		WherePrams sclassWhere=Method.where("isDelete", C.EQ, "N");
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("qc", qc);
        return new ResultObj(0, "正确", map);
	}
}





